/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}panel_RR_indexes.dta"
	CREATES:	${out_tables}/T21A_tobitreg_socioem.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/

	* Load data
	use "${dt_final}/panel_RR_indexes.dta", clear


	* Establish macros for regressions

/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
	
	* Outcomes
	*-----------------------------------------------------------
	local childoutcomes  "zDCCSPunt zptevir_irtscore2 zt_score zcbcl_t_e zcbcl_t_i zibatt_sspi zibatt_ssai zibatt_sssr zas_raw_score" 
	local behaviors "home_score se_score disc_score" 
	local beliefs "zpstyle1 zpstyle2 zpstyle3 zPSCS zPSSS_family zPSSS_friends zPSSS_others zPSI_Distress zCESD zpacotis"	
	local child "zptevir_irtscore2 zcog_index zsoc_index" 

	* Controls
	*-----------------------------------------------------------

	macro  def Xvar1 "i.age_year i.gender"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
	macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo"  

	
	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------

	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"
	

/*******************************************************************************
Table A34: Heterogeneity of impact: Facilitator Background
*******************************************************************************/


	*-------------------------------------
	* Create multilevel facilitator background
	*-------------------------------------

    set sortseed 	11122015
	set seed 		11122015
	gen fac_profmiss=0 if fac_profesion!=. & FOLIO_UC!=.
    replace fac_profmiss=0 if fac_profesion==. & FOLIO_UC!=.& TIPO==1
    replace fac_profmiss=1 if fac_profesion==. & FOLIO_UC!=.& TIPO!=1
		
	bys CENTRO_SALUD TIPO: gen centrotipo=_n	

	tab fac_profesion TIPO if centrotipo==1, col

	gen fac_prof=fac_profesion if FOLIO_UC!=.

	gen randb = uniform() if centrotipo==1 & TIPO==2
	replace fac_prof=cond(randb < .41, 1, cond(randb < .62, 2, cond(randb <0.76,3,4))) if fac_profesion==. & centrotipo==1 & TIPO==2
	tab fac_prof TIPO if centrotipo==1 & TIPO==2 & fac_profmiss==1

	gen randi = uniform() if centrotipo==1 & TIPO==3
	replace fac_prof=cond(randi < .27, 1, cond(randi < .5, 2, cond(randi <0.89,3,4))) if fac_profesion==. & centrotipo==1 & TIPO==3
	tab fac_prof TIPO if centrotipo==1 & TIPO==3 & fac_profmiss==1

	br CENTRO_SALUD TIPO fac_profesion fac_prof centrotipo fac_profmiss

	bys CENTRO_SALUD TIPO: egen auxfac_prof=max(fac_prof)
	replace fac_prof=auxfac_prof if fac_prof==.

	gen fac_nurse=.
	replace fac_nurse=0 if TIPO==1
	replace fac_nurse=1 if TIPO==2 & fac_prof==1
	replace fac_nurse=2 if TIPO==2 & fac_prof!=1
	replace fac_nurse=3 if TIPO==3 & fac_prof==1
	replace fac_nurse=4 if TIPO==3 & fac_prof!=1


	gen fac_psycho=.
	replace fac_psycho=0 if TIPO==1
	replace fac_psycho=1 if TIPO==2 & fac_prof==2
	replace fac_psycho=2 if TIPO==2 & fac_prof!=2
	replace fac_psycho=3 if TIPO==3 & fac_prof==2
	replace fac_psycho=4 if TIPO==3 & fac_prof!=2


	gen fac_teacher=.
	replace fac_teacher=0 if TIPO==1
	replace fac_teacher=1 if TIPO==2 & fac_prof==3
	replace fac_teacher=2 if TIPO==2 & fac_prof!=3
	replace fac_teacher=3 if TIPO==3 & fac_prof==3
	replace fac_teacher=4 if TIPO==3 & fac_prof!=3

	gen fac_swork=.
	replace fac_swork=0 if TIPO==1
	replace fac_swork=1 if TIPO==2 & fac_prof==4
	replace fac_swork=2 if TIPO==2 & fac_prof!=4
	replace fac_swork=3 if TIPO==3 & fac_prof==4
	replace fac_swork=4 if TIPO==3 & fac_prof!=4


	** Health worker 
	gen fac_healthw=.
	replace fac_healthw=0 if TIPO==1
	replace fac_healthw=1 if TIPO==2 & (fac_prof==1 | fac_prof==2)
	replace fac_healthw=2 if TIPO==2 & (fac_prof==3 | fac_prof==4)
	replace fac_healthw=3 if TIPO==3 & (fac_prof==1 | fac_prof==2)
	replace fac_healthw=4 if TIPO==3 & (fac_prof==3 | fac_prof==4)

	*-------------------------------------
	* Regression
	*-------------------------------------

	local finaloutcomes "zptevir_irtscore2 zcog_index zsoc_index" 
	foreach var of local finaloutcomes {

		xi: xtreg `var' i.fac_healthw $Xvar1 i.fac_profmiss, fe i(CENTRO_SALUD)
		estimates store `var'_healthw
		test _Ifac_healt_1 -_Ifac_healt_2 ==0
		estadd scalar p2=r(p)
		test _Ifac_healt_3 -_Ifac_healt_4==0
		estadd scalar p3=r(p)


	}


esttab zptevir_irtscore2_healthw zcog_index_healthw zsoc_index_healthw using "${out_tables}/appendix/T34A_facilitator_background.csv", replace f ///
      cells("b(fmt(3)star)" "se(fmt(3)par)") ///
      stats(r2 p2 p3  N, fmt(2 3 3 0)  ///
	  labels("R-squared" "P-value Nurse=Others: NEP-B" "P-value Nurse=Others: NEP-I" "Observations")) legend 	///
	  keep(_Ifac_healt_1  _Ifac_healt_2 _Ifac_healt_3  _Ifac_healt_4 ) ///
	  mtitles("Vocabulary Index" "Exec. Function Index"	"Socioemotional Index" ) ///
	  varlabels(_Ifac_healt_1 "NEP-B Health Worker" _Ifac_healt_2 "NEP-B Non health Worker" _Ifac_healt_3 "NEP-I Health Worker" _Ifac_healt_4 "NEP-I Non health Worker")

		
	
